<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/4/17
 * Time: 11:09
 */

namespace app\api\controller;

use think\Db;

class Privateaddress extends Base
{
    public $client_id;

    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $this->client_id = input('client_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    /**
     * 添加收货地址
     * @return \think\response\Json
     * addr_user_name        收货人
     * addr_user_phone       手机号
     * addr_sheng            省ID
     * addr_shi              市ID
     * addr_qu               区ID
     * addr_address          详细地址
     * addr_is_default       是否是默认地址   1 默认   2 非默认
     * client_id             客户ID
     */
    public function addAddr(){
        $addr_user_name = input('addr_user_name');
        $addr_user_phone = input('addr_user_phone');
        $addr_sheng = input('addr_sheng');
        $addr_shi = input('addr_shi');
        $addr_qu = input('addr_qu');
        $addr_address = input('addr_address');
        $addr_is_default = input('addr_is_default'); // 1 默认   2 非默认

        $insert = [
            'addr_user_id' => $this->user_id,
            'addr_client_id' => $this->client_id,
            'addr_user_name' => $addr_user_name,
            'addr_user_phone' => $addr_user_phone,
            'addr_sheng' => $addr_sheng,
            'addr_shi' => $addr_shi,
            'addr_qu' => $addr_qu,
            'addr_address' => $addr_address,
            'addr_is_default' => $addr_is_default,
            'addr_time' => $this->create_time
        ];

        Db::startTrans();
        try{
            // 如果是默认地址，则需先将所有地址设置成非默认
            if($addr_is_default == 1){
                Db::table('up_private_address')
                    ->where('addr_client_id', $this->client_id)
                    ->where('addr_is_default', 1)
                    ->setField('addr_is_default', 2);
            }
            $addr_id = Db::table('up_private_address')->insertGetId($insert);
            Db::commit();
            return json(['status' => 200, 'msg' => $addr_id]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 收货地址列表
     * @return \think\response\Json
     */
    public function addrList(){
        if($this->client_id){
            $where['addr_client_id'] = $this->client_id; // 客户收货地址列表
        }else{
            $where['addr_user_id'] = $this->user_id; // 用户->客户收货地址列表
        }
        $where['addr_del'] = 1;
        $info = $this->address(1, $where);
        if($info){
            return json(['status' => 200, 'msg' => $info]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }


    /**
     * 收货地址详情
     * @return \think\response\Json
     * addr_id     收货地址ID
     */
    public function showAddr(){
        $addr_id = input('addr_id');
        $where['addr_del'] = 1;
        if(isset($addr_id) && $addr_id != ''){ // 展示收货地址
            $where['addr_id'] = $addr_id;
        }else{ // 展示默认收货地址
            $where['addr_client_id'] = $this->client_id;
            $where['addr_is_default'] = 1;
        }
        $info = $this->address(2, $where);
        if($info){
            return json(['status' => 200, 'msg' => $info]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }


    /**
     * 编辑收货地址
     * @return \think\response\Json
     * addr_id               收货地址ID
     * addr_user_name        收货人
     * addr_user_phone       手机号
     * addr_sheng            省ID
     * addr_shi              市ID
     * addr_qu               区ID
     * addr_address          详细地址
     * addr_is_default       是否是默认地址   1 默认   2 非默认
     */
    public function updateAddr(){
        $addr_id = input('addr_id');
        $addr_user_name = input('addr_user_name');
        $addr_user_phone = input('addr_user_phone');
        $addr_sheng = input('addr_sheng');
        $addr_shi = input('addr_shi');
        $addr_qu = input('addr_qu');
        $addr_address = input('addr_address');
        $addr_is_default = input('addr_is_default'); // 1 默认   2 非默认

        $update = [
            'addr_user_name' => $addr_user_name,
            'addr_user_phone' => $addr_user_phone,
            'addr_sheng' => $addr_sheng,
            'addr_shi' => $addr_shi,
            'addr_qu' => $addr_qu,
            'addr_address' => $addr_address,
            'addr_is_default' => $addr_is_default,
            'addr_updatetime' => $this->create_time
        ];
        $default = Db::table('up_private_address')->where('addr_id', $addr_id)->value('addr_is_default');
        Db::startTrans();
        try{
            // 如果是默认地址，则需先将所有地址设置成非默认
            if($addr_is_default == 1 && $default == 2){
                Db::table('up_private_address')
                    ->where('addr_client_id', $this->user_id)
                    ->where('addr_is_default', 1)
                    ->setField('addr_is_default', 2);
            }
            Db::table('up_private_address')->where('addr_id', $addr_id)->update($update);
            Db::commit();
            return json(['status' => 200, 'msg' => '编辑成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 删除收货地址
     * @return \think\response\Json
     * addr_id       收货地址ID
     */
    public function delAddr(){
        $addr_id = input('addr_id');
        $where['addr_client_id'] = $this->user_id;
        $where['addr_del'] = 1;
        $addr = $this->address(3, $where);
        if($addr == 1){
            return json(['status' => 0, 'msg' => '最少要留一个收货地址']);
        }

        $res = Db::table('up_private_address')->where('addr_id', $addr_id)->setField('addr_del', 2);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }

    /**
     * 设置默认发货地址
     * @return \think\response\Json
     * addr_id      ID
     */
    public function setDefault(){
        $addr_id = input('addr_id');
        $default = Db::table('up_private_address')->where('addr_id', $addr_id)->value('addr_is_default');
        if($default == 1){
            return json(['status' => 200, 'msg' => '设置成功']);
        }else{
            Db::startTrans();
            try{
                Db::table('up_private_address')
                    ->where('addr_client_id', $this->user_id)
                    ->where('addr_is_default', 1)
                    ->setField('addr_is_default', 2);
                Db::table('up_private_address')->where('addr_id', $addr_id)->setField('addr_is_default', 1);
                Db::commit();
                return json(['status' => 200, 'msg' => '设置成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }
    }

    /**
     * 用户收货地址
     * @param int $type         1 列表    2 单条   3 统计
     * @param int $where        查询条件
     */
    private function address($type = 1, $where = []){
        $query = Db::table('up_private_address')
            ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
            ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
            ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
            ->where($where)
            ->field('addr_id, addr_user_name, addr_user_phone, addr_client_id, addr_sheng, addr_shi, addr_qu, 
            addr_address, addr_is_default, u1.district sheng, u2.district shi, u3.district qu');
        if($type == 1){
            $data = $query->order('addr_is_default asc, addr_time desc')->page($this->page, 10)->select();
        }elseif($type == 2){
            $data = $query->find();
        }else{
            $data = $query->count();
        }
        return $data;
    }
}